package com.huijutao.shoucangfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huijutao.MyApp;
import com.huijutao.R;
import com.huijutao.activityContent.FindActivity;
import com.huijutao.beans.FindItem;
import com.lidroid.xutils.exception.DbException;

import java.util.List;

/**
 * Created by aaa on 15-4-27.
 */
public class FMFindShouCang extends Fragment {
    private FindAdapter adapter;

    private List<FindItem> findItems;
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.list,container,false);
        listView = (ListView) view.findViewById(R.id.list_list);
        try {
            findItems = MyApp.dbUtils.findAll(FindItem.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        if (findItems !=null&& findItems.size()>=0) {
            adapter = new FindAdapter(findItems);
            listView.setAdapter(adapter);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), FindActivity.class);
                i.putExtra(FindActivity.DATA, findItems.get(position));
                startActivity(i);
            }
        });
        return view;
    }
    class FindAdapter extends BaseAdapter {
        private List<FindItem> data;

        FindAdapter(List<FindItem> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder ;
            if (convertView==null){
                holder=new ViewHolder();
                convertView= LayoutInflater.from(getActivity()).inflate(R.layout.shoucang_list_adapter, null);
                holder.img= (ImageView) convertView.findViewById(R.id.shoucang_list_img);
                holder.title= (TextView) convertView.findViewById(R.id.shoucang_list_title);
                holder.price=(TextView) convertView.findViewById(R.id.shoucang_list_price);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }
            FindItem newsdata = data.get(position);
            String title=newsdata.getTitle();
            String price=newsdata.getPrice();
            String img=newsdata.getImg_path();
            MyApp.mImageXutils.display(holder.img, img);
            holder.title.setText(title);
            holder.price.setText("¥" +price);
            return convertView;
        }
        class ViewHolder{
            private TextView title;
            private ImageView img;
            private TextView price;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            findItems = MyApp.dbUtils.findAll(FindItem.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        if (findItems !=null&& findItems.size()>=0) {
            adapter = new FindAdapter(findItems);
            listView.setAdapter(adapter);
        }
    }
}
